package com.hzw.saas.service.msg.service;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.msg.IMsgOperationService;
import com.hzw.saas.api.msg.bo.MsgActionBo;
import com.hzw.saas.api.msg.bo.MsgOperationBo;
import com.hzw.saas.api.msg.query.MsgOperationQuery;
import com.hzw.saas.api.msg.query.MsgOperationUQuery;
import com.hzw.saas.service.msg.mapper.MsgOperationMapper;
import com.hzw.saas.service.msg.model.MsgOperation;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;

/**
 * <p>
 *
 * </p>
 *
 * @author sonam
 * @since 2021/1/7 0:31
 */
@Slf4j
@RequiredArgsConstructor
@Service("msgOperationServiceImpl")
public class MsgOperationServiceImpl extends ServiceImpl<MsgOperationMapper, MsgOperation> implements IMsgOperationService, IService<MsgOperation> {
    private final static String CACHE_NAME = "msg:operator";
    private final MapperFacade mapperFacade;

    @Cacheable(cacheNames = {CACHE_NAME}, key = "#id")
    @Override
    public MsgOperationBo getMsgOperatorById(String id) {
        MsgOperation msgOperation = getById(id);
        if(msgOperation == null) {
            return null;
        }

        MsgOperationBo msgOperationDto = mapperFacade.map(msgOperation, MsgOperationBo.class);
        String action = msgOperation.getAction();
        msgOperationDto.setMsgActionDtoList(MsgActionBo.parse(action));
        msgOperationDto.setAction(null);

        return msgOperationDto;
    }

    @CachePut(cacheNames = {CACHE_NAME}, key = "#msgOperationParam.id ")
    @Override
    public MsgOperationBo addMsgOperator(MsgOperationQuery msgOperationParam) {
        MsgOperation msgOperation = mapperFacade.map(msgOperationParam, MsgOperation.class);
        msgOperation.setCreateDate(new Date());
        save(msgOperation);
        MsgOperationBo msgOperationDto = mapperFacade.map(msgOperation, MsgOperationBo.class);
        try {
            List<MsgActionBo> msgActionDtoList = MsgActionBo.parse(msgOperationDto.getAction());
            msgOperationDto.setMsgActionDtoList(msgActionDtoList);
        } catch (Exception e) {
            // ignore
        }
        return msgOperationDto;
    }

    @CacheEvict(cacheNames = {CACHE_NAME}, key = "#msgOperation.id")
    @Override
    public void updateMsgOperatorById(MsgOperationUQuery msgOperation) {
        updateById(mapperFacade.map(msgOperation, MsgOperation.class));
    }

    @CacheEvict(cacheNames = {CACHE_NAME}, key = "#msgOperation.id")
    @Override
    public void deleteMsgOperatorById(String id) {
        removeById(id);
    }

    @Override
    public void deleteMsgOperatorByMsgIds(List<String> msgIds) {
        if(CollectionUtils.isNotEmpty(msgIds)) {
            remove(Wrappers.<MsgOperation>lambdaQuery().in(MsgOperation::getMsgId, msgIds));
        }
    }

}
